package com.maigu.service.impl;

import com.maigu.dto.OrderDTO;
import com.maigu.enums.ResultEnum;
import com.maigu.exception.SellException;
import com.maigu.service.BuyerService;
import com.maigu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private OrderService orderService;
    @Override
    public OrderDTO findOrderOne(String openid, String orderId) {
        OrderDTO orderDTO = orderService.findOne(orderId);
        if(orderDTO==null)
        {
            return null;
        }
        if(!orderDTO.getBuyerOpenid().equalsIgnoreCase(openid))
        {
            log.error("[查询订单]订单的OPENID不一致，openid={},orderDTO={}",openid,orderDTO);
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        return orderDTO;
    }

    @Override
    public OrderDTO cancelOrder(String openid, String orderId) {
        OrderDTO orderDTO = orderService.findOne(orderId);
        if(orderDTO==null)
        {
            log.error("[取消订单]查不到改订单，openid={}",openid);
            throw new SellException(ResultEnum.ORDERDETAIL_NOT_EXIST);
        }
        if(!orderDTO.getBuyerOpenid().equalsIgnoreCase(openid))
        {
            log.error("[查询订单]订单的OPENID不一致，openid={},orderDTO={}",openid,orderDTO);
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        orderDTO=orderService.cancel(orderDTO);
        return orderDTO;
    }
}
